﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.ComponentModel;

namespace DevToolsRepo
{
	public class VersionInfo
	{
		public string Name { get; set; }

		[XmlIgnore]
		[Browsable(false)]
		public string PhysicalPath
		{
			get
			{
				return Path.Combine(Application.StartupPath, Path.Combine("files", Name));
			}
		}

		public string Size
		{
			get
			{
				var fileInfo = new FileInfo(PhysicalPath);
				if (!fileInfo.Exists)
					return "not exists";
				return SizeToString(fileInfo.Length);
			}
		}

		[XmlIgnore]
		[Browsable(false)]
		public ToolInfo ToolInfo { get; set; }

		public VersionInfo()
		{

		}
		public VersionInfo(ToolInfo toolInfo, FileInfo fileInfo)
		{
			Name = fileInfo.Name;
			ToolInfo = toolInfo;

			var physicalPath = PhysicalPath;
			var physicalFileInfo = new FileInfo(physicalPath);
			if (!physicalFileInfo.Directory.Exists)
				physicalFileInfo.Directory.Create();
			fileInfo.CopyTo(physicalPath, true);
		}

		public static string SizeToString(double size)
		{
			if (size < 1024)
				return size + " B";
			size = size / 1024;
			if (size < 1024)
				return size.ToString("#.00") + " KB";
			size = size / 1024;
			if (size < 1024)
				return size.ToString("#.00") + " MB";
			size = size / 1024;
			return size.ToString("#.00") + " GB";
		}
	}
}
